package Utilities;

import DTO.KhachHangDTO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DBUtils {

    public static Connection makeConnection() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://localhost:1433;databaseName=XMLProject;";
            Connection con = DriverManager.getConnection(url, "sa", "123456789");
            return con;
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }


    public static KhachHangDTO[] searchlikeName(String name) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = makeConnection();
            String sql = "Select * From KhachHang where CustomerName like ?";
            stm = con.prepareStatement(sql);
            stm.setString(1, "%" + name + "%");
            List list = new ArrayList();
            rs = stm.executeQuery();
            while (rs.next()) {
                int CustomerCode = rs.getInt("CustomerCode");
                String CustomerName = rs.getString("CustomerName");
                int IdentifyNumber = rs.getInt("IdentifyNumber");
                int Phone = rs.getInt("Phone");
                String Email = rs.getString("Email");
                int DiemTichLuy = rs.getInt("DiemTichLuy");
                KhachHangDTO stud = new KhachHangDTO(CustomerCode, CustomerName, IdentifyNumber, Phone, Email, DiemTichLuy);
                list.add(stud);
            }
            KhachHangDTO[] result = new KhachHangDTO[list.size()];
            list.toArray(result);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (con != null) {
                    con.close();
                }
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    public static boolean deleteUser(String username) {
        Connection con = null;
        PreparedStatement stm = null;

        try {
            con = makeConnection();
            String sql = "Delete From KhachHang where CustomerCode = ? ";
            stm = con.prepareStatement(sql);
            stm.setInt(1, Integer.parseInt(username));
            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {

                if (con != null) {
                    con.close();
                }
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return false;
    }
//    public static boolean updateUser(String CustCode, String CustName,String Email,int Phone, int Diem, int IdenNum) {
//        Connection con = null;
//        PreparedStatement stm = null;
//
//        try {
//            con = makeConnection();
//            String sql = "Update Users set password = ? , isAdmin = ? where userName = ? ";
//            stm = con.prepareStatement(sql);
//            stm.setString(1, password);
//            stm.setBoolean(2, role);
//            stm.setString(3, username);
//            int row = stm.executeUpdate();
//            if (row > 0) {
//                return true;
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//
//                if (con != null) {
//                    con.close();
//                }
//                if (stm != null) {
//                    stm.close();
//                }
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//
//        }
//        return false;
//
//    }
}
